package net.fdukedom.epicurus.client;

import net.fdukedom.epicurus.domain.entity.User;
import net.fdukedom.epicurus.domain.util.UserHelper;

public class ClientSession {
	/**
	 * Logged in user id.
	 */
	private int userId = -1;

	/**
	 * 
	 */
	private static ClientSession session;

	/**
	 * Default constructor.
	 */
	private ClientSession() {
	}

	/**
	 * Returns session object instance
	 * 
	 * @return session object instance
	 */
	public static ClientSession getInstance() {
		if ( session == null){
			session = new ClientSession();
		}
		return session;
	}

	/**
	 * Returns currently logged in user.
	 * 
	 * @return currently logged in user
	 */
	public User getUser() {
		return UserHelper.getInstance().load(userId);
	}

	/**
	 * Open user session.
	 * 
	 * @param userId
	 *            user id
	 */
	public void openSession(int userId) {
		this.userId = userId;
	}

	/**
	 * Close session
	 */
	public void closeSession() {
		this.userId = -1;
	}
	
	/**
	 * Returns true is user is logged in and false otherwise
	 * @return true is user is logged in, and false otherwis
	 */
	public boolean IsOpen() {
		return userId != -1;
	}
}
